﻿using System;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace Songhay.Data.GenericWeb.OutputDefinitions
{
    using Songhay.Data.GenericWeb.Procedures;

    /// <summary>
    /// Output procedures from the DBMS
    /// for <see cref="System.Xml.XPath.IXPathNavigable"/> types.
    /// </summary>
    public static partial class XPathNavigableOutput
    {
        /// <summary>
        /// Output new Fragment data for editing scenarios.
        /// </summary>
        /// <param name="commonConnection">The common connection.</param>
        /// <returns>Returns <see cref="System.Xml.XPath.XPathDocument"/>.</returns>
        public static XPathDocument NewFragment(DbConnection commonConnection)
        {
            string documents = List.Documents(commonConnection);
            string fragments = List.FragmentsForEdit(commonConnection);

            XPathDocument d = null;
            using(MemoryStream stream = new MemoryStream())
            {
                using(XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                {

                    writer.WriteStartDocument();
                    writer.WriteStartElement("ServerData");

                    writer.WriteStartElement("Fragment");

                    writer.WriteStartElement("FragmentId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("FragmentName");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("FragmentDisplayName");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("SortOrdinal");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("ItemChar");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("ItemText");
                    writer.WriteFullEndElement();

                    writer.WriteElementString("CreateDate", DateTime.Now.ToString(CommandHandler.GlobalDateFormat));

                    writer.WriteStartElement("EndDate");
                    writer.WriteFullEndElement();

                    writer.WriteElementString("ModificationDate", DateTime.Now.ToString(CommandHandler.GlobalDateFormat));

                    writer.WriteStartElement("DocumentId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("PrevFragmentId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("NextFragmentId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("IsPrevious");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("IsNext");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("IsWrapper");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("ClientId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("IsNext");
                    writer.WriteFullEndElement();

                    writer.WriteElementString("IsActive", "true");

                    writer.WriteFullEndElement();

                    writer.WriteRaw(documents);
                    writer.WriteRaw(fragments);

                    writer.WriteFullEndElement();
                    writer.WriteEndDocument();

                    writer.Flush();

                    stream.Position = 0;
                    d = new XPathDocument(stream);
                }
            }

            return d;
        }
    }
}
